package engine.gui.elements;

import engine.gui.Action;
import engine.gui.Component;
import engine.gui.GUIStyle;

public class Checkbox extends Component{
	private static final int TEXT_OFFSET = 3;
	
	private boolean checked;
	private Action action;
	
	private String text;
	
	public Checkbox() {
	}
	
	public Checkbox(String text) {
		this();
		this.text = text;
	}
	
	public Checkbox(boolean checked) {
		this();
		this.checked = checked;
	}
	
	public Checkbox(String text, boolean checked) {
		this(text);
		this.checked = checked;
	}
	
	public Checkbox(boolean checked, int posX, int posY, int width, int height) {
		this(checked);
		setPosX(posX);
		setPosY(posY);
		setWidth(height);
		setHeight(height);
	}
	
	public Checkbox(String text, boolean checked, int posX, int posY, int width, int height) {
		this(text, checked);
		setPosX(posX);
		setPosY(posY);
		setWidth(height);
		setHeight(height);
	}
	
	public Checkbox(int posX, int posY, int width, int height) {
		this();
		setPosX(posX);
		setPosY(posY);
		setWidth(height);
		setHeight(height);
	}
	
	public Checkbox(String text, int posX, int posY, int width, int height) {
		this(text);
		setPosX(posX);
		setPosY(posY);
		setWidth(height);
		setHeight(height);
	}
	
	@Override
	public void render(int x, int y, int width, int height) {
		GUIStyle style = getCurrentStyle();
		
		renderCheckBox(style, x, y);
		
		renderText(style, x, y);
	}
	
	private void renderCheckBox(GUIStyle style, int x, int y) {
		getWindowManager().getGuiShader().sendUniform4f("color", style.getCheckBoxBackgroundColor());
		
		if(style.getCheckBoxBackgroundImage() != null) {
			style.getCheckBoxBackgroundImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
		}
		
		if(checked) {
			getWindowManager().getGuiShader().sendUniform4f("color", style.getCheckBoxEnabledColor());
			
			if(style.getCheckBoxEnabledImage() != null) {
				style.getCheckBoxEnabledImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		} else {
			getWindowManager().getGuiShader().sendUniform4f("color", style.getCheckBoxDisabledColor());
			
			if(style.getCheckBoxDisabledImage() != null) {
				style.getCheckBoxDisabledImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		}
	}
	
	private void renderText(GUIStyle style, int x, int y) {
		if(text != null) {
			float h = style.getFont().getHeight();
			
			getWindowManager().getGuiShader().sendUniform4f("color", style.getCheckBoxTextColor());
			style.getFont().draw(text, x + getPosX() + getWidth() + TEXT_OFFSET, y + getPosY() + (h + getHeight()) / 2);
		}
	}
	
	@Override
	public void reset() {
		//do nothing
	}
	
	@Override
	public boolean mousePressed(int posX, int posY, int button) {
		if(button == 0 && posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() && posY < getPosY() + getHeight()) {
			checked = !checked;
			if(action != null) {
				action.performAction();
			}
			return true;
		}
		
		return false;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
}
